import java.util.Arrays;

public class MyList {
    public int[] elem;
    public int usedsize;
    public static final  int DEFAULT_SIZE = 10;

    public MyList() {
        this.elem =new int[DEFAULT_SIZE] ;
    }
    // 打印顺序表
    public void display() {
        for (int i = 0; i < this.usedsize; i++) {
            System.out.print(this.elem[i]+" ");
        }
    }
    // 新增元素,默认在数组最后新增
    public void add(int data) {
        if(isFull()){
          this.elem =  Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[usedsize] = data;
        usedsize++;
    }
    public boolean isFull(){
        return this.usedsize >= this.elem.length;
    }
    // 在 pos 位置新增元素
    public void add(int pos, int data) throws Posillegal {
        if(isFull()){
            this.elem =  Arrays.copyOf(this.elem,2*this.elem.length);
        }
        if(pos < 0||pos > usedsize){
            throw new Posillegal("pos位置不合法");
        }
        for (int i = usedsize - 1; i >= pos ; i--) {
            this.elem[i+1] =this.elem[i];
        }
        this.elem[pos] = data;
        this.usedsize++;
    }
    public boolean isEmpty(){
        if(this.size() == 0){
            return true;
        }
        return false;
    }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
//        if(isEmpty()){
//            throw new Emptyillegal("该链表为空");
//        }
        for (int i = 0; i < this.usedsize; i++) {
            if(this.elem[i] == toFind){
                return true;
            }
        }
         return false;
    }
    // 查找某个元素对应的位置
    public int indexOf(int toFind)  {
        for (int i = 0; i < this.usedsize; i++) {
            if(this.elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }
    // 获取 pos 位置的元素
    public int get(int pos) throws Emptyillegal, Posillegal {
        if(isEmpty()){
            throw new Emptyillegal("该链表为空");
        }
        if(pos < 0||pos >= this.usedsize){
            throw new Posillegal("pos位置不合法");
        }

        return this.elem[pos];
    }
    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) throws Emptyillegal, Posillegal {
        if(isEmpty()){
            throw new Emptyillegal("该链表为空");
        }
        if(pos < 0||pos >= this.usedsize){
            throw new Posillegal("pos位置不合法");
        }
        this.elem[pos] = value;
    }
    //删除第一次出现的关键字key
    public void remove(int Key) throws Emptyillegal {
        if(isEmpty()){
            throw new Emptyillegal("该链表为空");
        }
        int pos = indexOf(Key);
        if(pos == -1){
            System.out.println("该链表不该数据");
            return;
        }
        for (int i = pos; i < usedsize; i++) {
            this.elem[i] = this.elem[i+1];
        }
        this.usedsize--;

    }
    // 获取顺序表长度
    public int size() {
        return this.usedsize;
    }
    // 清空顺序表
    public void clear() {
        this.usedsize = 0;
    }

}
